package com.easyapi.web.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyapi.common.contants.Constants;
import com.easyapi.common.enums.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.common.param.role.PageRoleParam;
import com.easyapi.common.param.role.RoleAddParam;
import com.easyapi.common.param.role.RoleOperationParam;
import com.easyapi.common.param.role.RolePermissionOperationParam;
import com.easyapi.common.utils.BaseResponse;
import com.easyapi.common.utils.ResultUtils;
import com.easyapi.common.vo.user.PageRoleVo;
import com.easyapi.common.vo.user.PermissionVo;
import com.easyapi.server.service.user.RoleService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author liujunjie
 */
@RestController
@RequestMapping(Constants.BASE_URL + "role")
@Api(tags = {"ERoleCenter"})
public class RoleController {
    @Resource
    private RoleService roleService;

    /**
     * 新建|更新角色
     *
     * @param param
     * @return
     */
    @PostMapping("/saveRole")
    public BaseResponse<Boolean> saveRole(@RequestBody RoleAddParam param) {
        Long id = param.getId();
        String roleName = param.getRoleName();
        if (Objects.isNull(id) && StringUtils.isEmpty(roleName)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        roleService.saveRole(param);
        return ResultUtils.success(true);
    }

    @PostMapping("/pageRole")
    public BaseResponse<Page<PageRoleVo>> pageRole(@RequestBody PageRoleParam param) {
        if (Objects.nonNull(param.getRoleType()) && param.getRoleType() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (Objects.nonNull(param.getRoleId()) && param.getRoleId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        Page<PageRoleVo> page = roleService.page(param);
        return ResultUtils.success(page);
    }

    @PostMapping("/deleteRole")
    public BaseResponse<Boolean> deleteRole(@RequestBody RoleOperationParam param) {
        if (Objects.isNull(param.getId()) || param.getId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        roleService.deleteRole(param.getId());
        return ResultUtils.success(true);
    }

    @PostMapping("/listPermission")
    public BaseResponse<List<PermissionVo>> listPermission(@RequestBody RoleOperationParam param) {
        if (Objects.isNull(param.getId()) || param.getId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<PermissionVo> permissionVos = roleService.listPermissionsByRoleId(param.getId());
        return ResultUtils.success(permissionVos);
    }

    @PostMapping("/listRoleExPermission")
    public BaseResponse<List<PermissionVo>> listRoleExPermission(@RequestBody RoleOperationParam param) {
        if (Objects.isNull(param.getId()) || param.getId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<PermissionVo> permissionVos = roleService.listRoleExPermission(param.getId());
        return ResultUtils.success(permissionVos);
    }

    @PostMapping("/addRolePermission")
    public BaseResponse<Boolean> addRolePermission(@RequestBody RolePermissionOperationParam param) {
        Long roleId = param.getRoleId();
        Long permissionId = param.getPermissionId();
        if (Objects.isNull(roleId) || roleId <= 0 || Objects.isNull(permissionId) || permissionId <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        roleService.addRolePermission(roleId, permissionId);
        return ResultUtils.success(true);
    }

    @PostMapping("/deleteRolePermission")
    public BaseResponse<Boolean> deleteRolePermission(@RequestBody RolePermissionOperationParam param) {
        Long roleId = param.getRoleId();
        Long permissionId = param.getPermissionId();
        if (Objects.isNull(roleId) || roleId <= 0 || Objects.isNull(permissionId) || permissionId <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        roleService.delRolePermission(roleId, permissionId);
        return ResultUtils.success(true);
    }
}
